#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#       fasta.py
#       
#       Copyright 2010 Brandon Invergo <brandon@brandoninvergo.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.


from gettext import gettext as _

import gtk
import gedit

# Menu item example, insert a new item in the Tools menu
ui_str = """<ui>
  <menubar name="MenuBar">
    <menu name="EditMenu" action="Edit">
      <placeholder name="EditOps_2">
        <menuitem name="sequenceMask" action="sequenceMask"/>
      </placeholder>
    </menu>
  </menubar>
</ui>
"""
class sequenceMaskWindowHelper:
    def __init__(self, plugin, window, maskchar):
        self._window = window
        self._plugin = plugin
        # Insert menu items
        self._insert_menu()
        self._maskchar = maskchar

    def deactivate(self):
        # Remove any installed menu items
        self._remove_menu()
        self._window = None
        self._plugin = None
        self._action_group = None

    def _insert_menu(self):
        # Get the GtkUIManager
        manager = self._window.get_ui_manager()
        # Create a new action group
        self._action_group = gtk.ActionGroup("sequenceMaskPluginActions")
        self._action_group.add_actions([("sequenceMask", None, 
            _("Mask sequence region"), None, 
            _("Mask an erroneous region in a biological sequence or alignment"),
            self.on_format_document_activate)])
        # Insert the action group
        manager.insert_action_group(self._action_group, -1)
        # Merge the UI
        self._ui_id = manager.add_ui_from_string(ui_str)

    def _remove_menu(self):
        # Get the GtkUIManager
        manager = self._window.get_ui_manager()
        # Remove the ui
        manager.remove_ui(self._ui_id)
        # Remove the action group
        manager.remove_action_group(self._action_group)
        # Make sure the manager updates
        manager.ensure_update()

    def update_ui(self):
        self._action_group.set_sensitive(self._window.get_active_document() != None)

    # Menu activate handlers
    def on_format_document_activate(self, action):
        doc = self._window.get_active_document()
        view = self._window.get_active_view()
        if not doc:
            return
        start_iter, end_iter= doc.get_selection_bounds()
        if start_iter is not None and end_iter is not None:
        # Get all the text in the selection
            select_len = len(doc.get_text(start_iter, end_iter))
            doc.delete(start_iter, end_iter)
            mask_text = self._maskchar * select_len
            doc.insert_at_cursor(mask_text)
        
    def set_maskchar(self, maskchar):
        # Set the number of characters per line
        self._maskchar = maskchar
        
class sequenceMaskPlugin(gedit.Plugin):
    def __init__(self):
        gedit.Plugin.__init__(self)
        self._instances = {}
        self._maskchar = "N"

    def activate(self, window):
        self._instances[window] = sequenceMaskWindowHelper(self, window, 
            self._maskchar)

    def deactivate(self, window):
        self._instances[window].deactivate()
        del self._instances[window]

    def update_ui(self, window):
        self._instances[window].update_ui()

    def is_configurable(self):
        return True
    
    def create_configure_dialog(self):
        dialog = gtk.Dialog("Sequence Masker Plugin Configuration", 
            buttons=(gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE))
        maskchar_label = gtk.Label("Masking Character:  ")
        self._maskchar_entry = gtk.Entry()
        self._maskchar_entry.set_text(self._maskchar)
        self._maskchar_entry.set_max_length(1)
        self._maskchar_entry.set_width_chars(1)
        maskchar_hbox = gtk.HBox()
        maskchar_hbox.pack_start(maskchar_label)
        maskchar_hbox.pack_start(self._maskchar_entry)
        maskchar_label.show()
        self._maskchar_entry.show()
        dialog.vbox.pack_start(maskchar_hbox)
        maskchar_hbox.show()
        dialog.connect("response", self.on_response)
        self.dialog = dialog
        return dialog
        
    def on_response(self, dialog, response_id):
        maskchar = self._maskchar_entry.get_text()
        self._maskchar = maskchar
        for window in self._instances.keys(): 
            self._instances[window].set_maskchar(maskchar)
        self.dialog.hide()
